package com.bosch.in.rtp1.ept.manager.impl;

import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.bosch.in.rtp1.ept.dao.AuthenticationDao;
import com.bosch.in.rtp1.ept.manager.AuthenticationManager;
import com.bosch.in.rtp1.ept.util.DaoFactory;

/**
 * Concrete implementation of AuthenticationManager which provides service to examine 
 * password information
 * DBConnectionManager attribute.  
 * @author TPR1HC
 *
 */
public class AuthenticationManagerImpl implements AuthenticationManager{
	/**
	 * used to manage connection
	 */
	private DBConnectionManager dbManager;
	/**
	 * DAO object to validate password.
	 */
	private AuthenticationDao authenticationDao;
	
	private static final Logger LOGGER = Logger.getLogger(AuthenticationManagerImpl.class);
	
	
	/**
	 * Look up datasource to get connection from pre-initialized connection pool
	 * @throws Exception
	 */
	public AuthenticationManagerImpl() { 
	}

	/**
	 * lazy initialize connection when we really need to interact with database and in case 
	 * data can't be found in caching system.
	 * @throws SQLException
	 */
	private void init() throws SQLException {
		dbManager = new DBConnectionManager();
		authenticationDao = DaoFactory.getInstance().getAuthenticationDao(dbManager.getConnection());
	}

	@Override
	public boolean validatePassword(String password) throws SQLException {
		LOGGER.debug("Begin validatePassword");
		try
		{
			// lazy init connection
			init();
			return authenticationDao.validatePassword(password);
		}
		finally
		{
			dbManager.close();
			LOGGER.debug("Closed connection");
			LOGGER.debug("End validatePassword");
		}
	}
}
